//
//  HWVipGiftBagCouponsSearchView.swift
//  HWanGame
//
//  Created by 王昌健 on 2021/12/14.
//  Copyright © 2021 94Wan. All rights reserved.
//

import UIKit

class HWVipGiftBagCouponsSearchView: UIView {
    var bgImageView:UIImageView!
    var searchLogo:UIImageView!
    var searchField:UITextField!
    var searchBgView:UIImageView!
    var searchGame:((_:String)->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

}

extension HWVipGiftBagCouponsSearchView {
    private func initUI() -> Void {
        bgImageView = UIImageView()
        bgImageView.image = UIImage(named: "giftBagSearchBg")
        addSubview(bgImageView)
        
        searchBgView = UIImageView()
//        searchBgView.backgroundColor = .colorWithHexString(hexString: "#F3F4F8")
        searchBgView.image = UIImage(named: "giftBagSearch")
        searchBgView.isUserInteractionEnabled = true
        addSubview(searchBgView)
        
        searchLogo = UIImageView()
        searchLogo.image = UIImage(named: "giftBagSearchLogo")
        searchBgView.addSubview(searchLogo)
        
        searchField = UITextField()
        searchField.textColor = .white
        searchField.clearButtonMode = .whileEditing
        searchField.delegate = self
        searchField.textColor = .colorWithHexString(hexString: "#333333")
        searchField.font = .systemFont(ofSize: 12)
        searchBgView.addSubview(searchField)
        
        let placeHoldLabel = UILabel.init()
        placeHoldLabel.text = "搜索游戏"
        placeHoldLabel.textColor = UIColor.colorWithHexString(hexString: "#999999")
        placeHoldLabel.font = UIFont.systemFont(ofSize: 12)
        searchField.setValue(placeHoldLabel, forKey: "_placeholderLabel")
        searchField.addTarget(self, action: #selector(fieldFieldChange(_:)), for: .editingChanged)
//        searchBgView.isHidden = true
    }
    
    private func initFrame() -> Void {
        bgImageView.snp.makeConstraints { make in
            make.left.top.equalToSuperview()
            make.right.equalTo(-13*widthScale)
            make.height.equalTo(bgImageView.snp.width).multipliedBy(0.11)
            make.bottom.equalToSuperview()
        }
        
        searchBgView.snp.makeConstraints { make in
            make.right.equalTo(-15*widthScale)
            make.top.equalTo(7*widthScale)
            make.bottom.equalTo(-7*widthScale)
            make.width.equalTo(screenWidth*0.61)
        }
        
        searchLogo.snp.makeConstraints { make in
            make.left.equalTo(10*widthScale)
            make.width.height.equalTo(12*widthScale)
            make.centerY.equalToSuperview()
        }
        
        searchField.snp.makeConstraints { make in
            make.left.equalTo(searchLogo.snp.right).offset(6*widthScale)
            make.centerY.equalToSuperview()
            make.right.equalTo(-10)
        }
    }
}

extension HWVipGiftBagCouponsSearchView:UITextFieldDelegate {
    @objc func fieldFieldChange(_ textField:UITextField) ->Void {
        if textField.text?.count ?? 0 > 0 {
//            placeHoldLabel.textColor = .white
//            lineViewOne.backgroundColor = .white
        }else {
//            placeHoldLabel.textColor = .colorWithHexString(hexString: "#999999")
//            lineViewOne.backgroundColor = .colorWithHexString(hexString: "#999999")
            self.searchGame?("")
        }
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.searchGame?(textField.text ?? "")
        return true
    }
}
